import HouseBusiness from '@/business/HouseBusiness';
import { useEffect } from 'react';
import authStore from '@/stores/AuthStore';
import houseStore from '@/stores/HouseStore';
import userStore from '@/stores/UserStore';

export default function useHouse() {
  const {user} = userStore;
  const {isLogin} = authStore;
  const {setLandlordHouseList, setHouseNumberTotal, setMarketHouseList} = houseStore;
  /**
   * 重新获取房东的房屋信息
   */
  const againGetLandlordHouseList = async () => {
    if (!isLogin) return;
    // 获取房屋信息
    const res: any = await HouseBusiness.getHouse();
    setLandlordHouseList(res);
    setHouseNumberTotal(res?.length || 0);
  };

  /**
   * 获取房东房屋列表
   */
  const useGetLandlordHouseList = () => {
    // 登录成功后获取房屋信息
    useEffect(() => {
      againGetLandlordHouseList();
    }, [isLogin, user?.id]);
  };

  /**
   * 获取市场房屋列表
   * @param minLat
   * @param maxLat
   * @param minLng
   * @param maxLng
   */
  const getMarketHouseList = async (
    minLat: number,
    maxLat: number,
    minLng: number,
    maxLng: number
  ) => {
    const res: any = await HouseBusiness.getHousesByPage(minLat, maxLat, minLng, maxLng);
    setMarketHouseList(res);
  };

  return {
    useGetLandlordHouseList,
    againGetLandlordHouseList,
    getMarketHouseList,
  };
}